//
//  ByCellQRCode.m
//  chequanbao_ios
//
//  Created by mc on 2017/5/8.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByCellQRCode.h"


#import "ByViewCardAndPayBig.h"

@implementation ByCellQRCode{

//    UIImageView         *_gradeImg;
//    UILabel             *_gradeLabel;
    UIView              *_bgView;
    
    UIImageView         *_advartImg;
    UILabel             *_nickNameLabel;
    UIImageView         *_sexImg;
    
//    UILabel             *_postionLabel;
    UILabel             *_referralCodeLabel;
    
    ByViewCardAndPayBig *_qrCodeView;
    
    UILabel             *_loadLabel;
    UIButton            *_copyBtn;
    
}

+ (instancetype)cellWithTableView:(UITableView*)tableView{
    
    static NSString *cellIdentifier = @"ByCellQRCode";
    ByCellQRCode *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self createViews];
    }
    return self;
}

- (void)createViews{
    _bgView = [[UIView alloc] init];
    _bgView.backgroundColor = [UIColor whiteColor];
    _bgView.layer.cornerRadius = 4.0;
    [self addSubview:_bgView];
    
//    _gradeImg = [[UIImageView alloc] init];
//    [self addSubview:_gradeImg];
    
//    _gradeLabel = [[UILabel alloc] init];
//    _gradeLabel.font = FONT(14);
//    [_bgView addSubview:_gradeLabel];
    
 
    
    _advartImg = [[UIImageView alloc] init];
//    _advartImg.layer.cornerRadius = 2.0;
    [_bgView addSubview:_advartImg];
    
    _nickNameLabel = [[UILabel alloc] init];
    _nickNameLabel.font = FONT(14);
    [_bgView addSubview:_nickNameLabel];
    
    _sexImg = [[UIImageView alloc] init];
    [_bgView addSubview:_sexImg];
    
//    _postionLabel = [[UILabel alloc] init];
//    _postionLabel.font = FONT(14);
//    [_bgView addSubview:_postionLabel];
    
    _referralCodeLabel = [[UILabel alloc] init];
    _referralCodeLabel.font = FONT(14);
    [_bgView addSubview:_referralCodeLabel];
    
    _qrCodeView = [[ByViewCardAndPayBig alloc] init];
    [_bgView addSubview:_qrCodeView];
    
    _loadLabel = [[UILabel alloc] init];
    [_bgView addSubview:_loadLabel];
    
    _copyBtn = [[UIButton alloc] init];
    [_copyBtn setImage:[UIImage imageNamed:@"card_paste"] forState:UIControlStateNormal];
    [_bgView addSubview:_copyBtn];
}


-(void)setModel:(ByModelCardAndPay *)model{
    _model = model;
    // 背景视图
    CGFloat x,y,width,height;
    x = 35;
    y = 10;
    width = SCREEN_WIDTH - 2*x;
    _bgView.frame = CGRectMake(x, y, width, 0);
    
    // 级别图标
//    x = SCREEN_WIDTH/2 - 10;
//    y = 0;
//    width = height = 20;
//    _gradeImg.frame = CGRectMake(x, y, width, height);
    
    // 级别
//    x = 0;
//    y = 10 + 5;
//    width = _bgView.width;
//    height = 20;
//    _gradeLabel.frame = CGRectMake(x, y, width, height);
    
    // todo(根据级别 区分)
//    _gradeImg.image = [UIImage imageNamed:@"card_grade_one"];
    
    // 头像
    x = 20;
    y = 10;
    width = height = 40;
    _advartImg.layer.cornerRadius = height / 2;
    _advartImg.layer.masksToBounds = YES;
    _advartImg.frame = CGRectMake(x, y, width, height);
    
    ByModelUser *user = [ByCacheUser getUserInfo];
    // 昵称
    x = _advartImg.right + 10;
    y = CGRectGetMinY(_advartImg.frame) + 10;
    width = [user.nickname getSizeOfString:14].width;
    CGFloat max_width = _bgView.width - x - 15 - 10;
    width = MIN(width, max_width);
    height = 20;
    _nickNameLabel.frame = CGRectMake(x, y, width, height);
    
    // 性别
    x = _nickNameLabel.right;
    width = height = 20;
    _sexImg.frame = CGRectMake(x, y, width, height);
    
    // todo
    if (user.sex.integerValue == 2) {
        _sexImg.image = [UIImage imageNamed:@"card_sex_male"];
    }else if(user.sex.intValue == 1){
        _sexImg.image = [UIImage imageNamed:@"card_sex_female"];
    }
    
    // 地址
//    x = _advartImg.right + 10;
//    y = _nickNameLabel.bottom + 5;
//    width = _bgView.width - x - 10;
//    height = 15;
//    _postionLabel.frame = CGRectMake(x, y, width, height);
    
    // 推荐码
    x = 20;
    y = _advartImg.bottom + 10;
    width = _bgView.width - x;
    height = 20;
    _referralCodeLabel.frame = CGRectMake(x, y, width, height);
    
    // 二维码
    x = 20;
    y = _referralCodeLabel.bottom + 10;
    width = height = _bgView.width - 2*x;
    _qrCodeView.frame = CGRectMake(x, y, width, height);
    
    // 推荐链接
    x = 20;
    y = _qrCodeView.bottom + 10;
    width = _bgView.width - 2*x - 20;
    height = 20;
    _loadLabel.frame = CGRectMake(x, y, width, height);
    
    // 复制按钮
    x = _loadLabel.right;
    width = height = 20;
    _copyBtn.frame = CGRectMake(x, y, width, height);
    [_copyBtn addTarget:self action:@selector(copyAction) forControlEvents:UIControlEventTouchUpInside];
    _bgView.height = CGRectGetMaxY(_copyBtn.frame) + 10;
    
    
    // 我的测试数据
    _nickNameLabel.text = user.nickname;
    if (_model.invite_code.length != 0) {
        _referralCodeLabel.text = [NSString stringWithFormat:@"我的推荐码：%@",_model.invite_code];
    }
    _loadLabel.text = _model.url;
    [_advartImg setImageWithURL:[NSURL URLWithString:user.head] placeholderImage:DEFAULT_IMAGE completed:nil];
    
//    UIApplication *appliction = [UIApplication sharedApplication];
//    AppDelegate *delegate =(AppDelegate*) appliction.delegate;
////    if (delegate.locationDic.count != 0) {
//        NSString *province = [delegate.locationDic objectForKey:@"province"];
//        NSString *city = [delegate.locationDic objectForKey:@"city"];
//        _postionLabel.text = [province stringByAppendingString:city];
//    }

    
    
    
}

// 设置二维码图片
-(void)setOutPutImg:(UIImage *)outPutImg{
    _outPutImg = outPutImg;
    _qrCodeView.bigImg = _outPutImg;
}

// 实现复制粘贴
- (void)copyAction{
    [ByCoreToolAlert showOnlyText:@"复制成功"];
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = _loadLabel.text;
}

@end
